package com.bytecollege.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bytecollege.model.Article;
import com.bytecollege.vo.AdminArticleVO;
import com.bytecollege.vo.ArticleDetailVo;
import com.bytecollege.vo.ArticleVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytecollege.vo.MyAreMarkVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName: ArticleMapper
 * @Description:
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
        /**
         * 讨论区帖子分页展示
         * @param page
         * @param wrapper
         * @return
         */
        @Select("select ar.article_id,ar.article_title,ar.article_time,ar.article_clicks,u.user_nickname,u.user_headphoto,at.atype_name,count(am.aremark_id) aremark_amount"+
                " from article ar" +
                "   left join user u" +
                "   on ar.user_id = u.user_id" +
                "   left join atype at" +
                "   on ar.atype_id = at.atype_id" +
                "   left join aremark am"+
                "   on ar.article_id = am.article_id" +
                "   ${ew.customSqlSegment}"+
                "   group by ar.article_id"+
                "   order by ar.article_time desc")
        IPage<ArticleVo> getAllArticles(Page<ArticleVo> page,@Param("ew") Wrapper wrapper);

        /**
         * 获取文章详细的相关信息
         * @param wrapper
         * @return
         */
        @Select("select ar.article_id,ar.article_title,ar.article_content,ar.article_time,ar.article_clicks,u.user_nickname,u.user_headphoto,count(am.aremark_id) aremark_amount"+
                " from article ar" +
                "   left join user u" +
                "   on ar.user_id = u.user_id" +
                "   left join aremark am"+
                "   on ar.article_id = am.article_id" +
                "   ${ew.customSqlSegment}"+
                "   group by ar.article_id")
        ArticleDetailVo getArticleById(@Param("ew") Wrapper wrapper);



        /***
         * @Description: 查询待审核的文章，根据时间排序，等待时间越久的文章显示的越前
         * @Param: [page]
         * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.bytecollege.vo.AdminArticleVo>
         * @Author: xigua
         * @Date: 2022/10/25
         */
        @Select("select a.article_id,a.article_title,a.article_content,a.article_time,a.article_state,u.user_name,t.atype_name" +
                " from article a" +
                "  left join atype t on a.atype_id=t.atype_id" +
                "  left join user u on a.user_id=u.user_id" +
                "  where a.article_state = 0" +
                "  order by a.article_time asc")
        Page<AdminArticleVO> getAllByCheck(Page<AdminArticleVO> page);

        /**
         * 帖子回复量榜单
         * @return
         */
        @Select("select row_number()over(order by count(am.aremark_id) desc)  order_id,ar.article_id,ar.article_title,count(am.aremark_id) aremark_amount"+
                " from article ar" +
                "   left join aremark am"+
                "   on ar.article_id = am.article_id" +
                "   where ar.article_state = 1"+
                "   group by ar.article_id"+
                "   limit 10")
        List<ArticleVo> getHotArticle();

        /**
         * 帖子访问量榜单
         * @return
         */
        @Select("select row_number()over(order by ar.article_clicks desc) order_id,ar.article_id,ar.article_title"+
                " from article ar" +
                "   where ar.article_state = 1"+
                "   order by ar.article_clicks desc"+
                "   limit 10")
        List<ArticleVo> getReadArticle();



        /**
         * 获取用户页用户对应的所有的文章
         * @return
         */
        @Select("select ar.article_id,ar.article_title,ar.article_content,ar.article_clicks"+
                " from article ar" +
                "   left join user u" +
                "   on ar.user_id = u.user_id" +
                "   where ar.article_state = 1"+
                "   and u.user_id = #{userId}"+
                "   order by ar.article_time desc")
        List<ArticleVo> getAllArticle(Page<ArticleVo> page , @Param("userId") Integer userId);
}
